<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>layui</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" th:href="@{/alllayui/layui/css/layui.css}"
	media="all">
<!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
	<div class="layui-form searchTable">
		<div class="layui-form-item">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">开始时间：</label>
					<div class="layui-input-inline">
						<input type="text" name="startDate" id="startDate"
							placeholder="请选择开始时间" class="layui-input datePicker">
					</div>

					<label class="layui-form-label">结束时间：</label>
					<div class="layui-input-inline">
						<input type="text" id="endDate" placeholder="请选择结束时间"
							class="layui-input datePicker">
					</div>

					<label class="layui-form-label" style='display: none;'>入库单类型：</label>
					<div class="layui-input-inline" style='display: none;'>
						<select id="documentType" name="documentType" lay-verify="required" lay-search="">
							<option value="Inbound">采购入库</option>
							<!-- <option value="ReturnInbound">退货入库</option>
							<option value="BorrowInbound">借调入库</option>
							<option value="0">全部</option> -->
						</select>
					</div>

				</div>

			</div>
		</div>



		<div class="layui-form-item">
			<!-- <label class="layui-form-label">未创建采购入库单</label>
						<div class="layui-input-inline">
							<input type="checkbox" checked="" name="close" lay-skin="switch"
								lay-text="ON|OFF" />
								
						</div> -->

			<label class="layui-form-label">入库单单号：</label>
			<div class="layui-input-inline">
				<input type="text" name="documentNumber" id="documentNumber" autocomplete="off"
					class="layui-input">
			</div>

			<label class="layui-form-label">CRN销售单单号：</label>
			<div class="layui-input-inline">
				<input type="text" id="salesOrderNumber" name="salesOrderNumber" autocomplete="off"
					class="layui-input">
			</div>

			<label class="layui-form-label"><button id="searchBtn" class="layui-btn" data-type="reload">搜索</button></label>
		</div>
	</div>


	<table class="layui-hide" id="Resources" lay-filter="Resources"></table>

	<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
       <shiro:hasPermission name="purchase/list">
        <button class="layui-btn layui-btn-sm" lay-event="add">导入数据</button>
       </shiro:hasPermission>
    </div>
</script>

	

	<script type="text/javascript">
    var btnCls = ['layui-btn-warm','layui-btn-danger','layui-btn-disabled','layui-btn-normal'];
	function dict(rowD,str){ 
		var ret = "";
		var btnIndex = 0;
		if(str=="type"){
			if(rowD.type==0){
				btnIndex = 0;
				ret = "启用";
			} 
			if(rowD.type==1){
				btnIndex = 1;
				ret = "禁用";
			} 
		}
		 
		var btn = "<a class=\"layui-btn layui-btn-xs layui-btn-radius "+ btnCls[btnIndex]+ "\">"+ ret +"</a>"; 
		return btn;
	}
</script>
	<script th:src="@{/alllayui/layui/layui.js}" charset="utf-8"></script>
	<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

	<script>
    layui.use(['jquery','table','form','laydate'], function(){
        var table = layui.table;
        var laydate = layui.laydate;
        var form = layui.form;
        var searchData=[];
        table.render({
            elem: '#Resources'
            ,url:'/purchase/list'
            	,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            ,method: 'post'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: 'Resources'
            ,page:false
            ,cols: [
                [
               /*  {field: 'id', title:'ID',  align:'center', width:"0%"}
              , */{field: 'b2bUnit', title:'渠道商编号',  align:'center', width:"10%"}
              ,{field: 'creationTime', title:'记录创建时间',  align:'center' ,width:"10%"}
              ,{field: 'documentNo', title:'单据表头编号',  align:'center', width:"10%" }
              ,{field: 'documentType', title:'单据类型',  align:'center' , width:"10%"}
              ,{field: 'documentTypeDesc', title:'单据类型描述',  align:'center', width:"10%" }
               ,{field: 'externalOrderNo', title:'渠道商自有系统的单据编号',  width:"10%"}
         
              ,{field: 'batchNumber', title:'批次号',  align:'center' , width:"10%"}
              ,{field: 'dueDate', title:'到期日期',  align:'center' , width:"10%"}
              ,{field: 'entryNumber', title:'单据行项目编号',  align:'center', width:"10%" }
              ,{field: 'materialNumber', title:'物料号',  align:'center'}
              ,{field: 'materialDesc', title:'物料描述',  align:'center' }
              ,{field: 'productionDate', title:'生产日期',  align:'center', unresize: true } 
              //,{field: 'type', title:'状态',  align:'center', templet: function(d){return dict(d,'type');}}
               //,{fixed: 'right', title:'操作', toolbar: '#barDemo'} 
            ]
            ]
           
           /*  ,request: {
                pageName: 'currentPageNum' //页码的参数名称，默认：page
                ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
            } */
            ,parseData: function(res){ //res 即为原始返回的数据
            	searchData = res.recordList;
                console.log("data:",res);
                return {
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": res.totalRecords, //解析数据长度
                    "data": res.recordList //解析数据列表
                }; 
               // return res.recordList;
            },done: function(){
            	 $("[data-field='id']").css('display','none'); 
            },limit: Number.MAX_VALUE 

        });

        laydate.render({
            elem: '#startDate'
          });
        
        laydate.render({
            elem: '#endDate'
          });
        
      //同时绑定多个
        lay('.datePicker').each(function(){
            laydate.render({
                elem: this
                ,trigger: 'click'
                ,type: 'datetime'
            });
        });

        
        
        var $ = layui.$, active = {
            reload: function(){
            	 var queryWhere = {}; 
            	 var startDate = $("#startDate").val();
                 if((startDate!="") && (startDate.length>0)){
                	 queryWhere.startDate = startDate;
                 }else{
                	 layer.msg('开始时间不能为空！', { icon: 7, time: 2000, shade: [0.6, '#000', true] });
                	 return;
                 } 
                 var endDate = $("#endDate").val();
                 if((endDate!="") && (endDate.length>0)){
                	 queryWhere.endDate = endDate;
                 }
                 var documentNumber = $("#documentNumber").val();
                 if((documentNumber!="") && (documentNumber.length>0)){
                	 queryWhere.documentNumber = documentNumber;
                 }
                 var salesOrderNumber = $("#salesOrderNumber").val();
                 if((salesOrderNumber!="") && (salesOrderNumber.length>0)){
                	 queryWhere.salesOrderNumber = salesOrderNumber;
                 }
                 var documentType = $("#documentType").val();
                 if((documentType!="") && (documentType.length>0)){
                	 queryWhere.documentType = documentType;
                 }
                 
                 console.log("queryWhere:",queryWhere);
                //执行重载
                table.reload('Resources', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , method: 'POST'
                    ,where:  queryWhere 
                    ,done: function(res, curr, count){
                    	this.where= {};
                    }
                }, 'data');
            }
        };

        $('.searchTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //头工具栏事件
        table.on('toolbar(Resources)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            //console.log("table:",table);
            switch(obj.event){
                //自定义头工具栏右侧图标 - 提示
                case 'add':
                    //iframe层-父子操作
                {
                   console.log("searchData :",searchData);
                   if(searchData.length==0){
                	   //layer.alert('暂无数据需要导入！');
                	   layer.msg('暂无数据需要导入！', { icon: 7, time: 2000, shade: [0.6, '#000', true] });
                	   break;
                   }
                   layer.confirm('确定导入数据吗？', function(index){ 
                	   //console.log("obj:",index);
                	   var loading = layer.load(0, {
                           shade: false
                       });
                	   $.ajax({
                            type:'POST',
                            url:'/purchase/implData',
	                       	dataType:"json",     
	                       	contentType:"application/json",  
	                       	data:JSON.stringify(searchData),
                            success:function(data){  
                            	console.log("data:",data);
                           		if(data.code==200){
                           			layer.close(loading);
                           			var errs=data.error;
                           			if(errs&&errs.length>0){
                           				var htm="<table style='min-height: 25px; line-height: 25px; text-align: center; border-color:#151515; border-collapse: collapse;' border='1'><tr><td width='100px'>编码</td><td width='300px'>名称</td><td width='250px'>描述</td></tr>";
                               			for(var i=0;i<errs.length;i++){
                               				htm +="<tr><td>"+errs[i].fcode+"</td>";
                               				htm +="<td>"+errs[i].fname+"</td>";
                               				htm +="<td>"+errs[i].desc+"</td></tr>";
                               			}
                               			htm +="</table>";
                               			console.log('htm = '+htm);
                                       layer.open({
                                    	   area: ['700px', '350px'],
                                    	   fixed: false, //不固定
                                           maxmin: true,
                                    	   title: '导入结果'
                                    	   ,content: htm
                                    	 }); 
                           			}else{
                           				layer.msg('导入成功！', { icon: 1, time: 2000, shade: [0.6, '#000', true] });
                           			}
                           			
                           		}else{
                           			layer.msg('导入异常，请联系管理员！', { icon: 2, time: 2000, shade: [0.6, '#000', true] });
                           		}
                       		}
                      	});
                	   layer.close(index);
                   });
                }
                    break;
                
            };
        });

        //监听行工具事件
        table.on('tool(Resources)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){ 
                    $.post("/resources/delete",{"id":data.id},function (data) {
                        if(data.message=='SUCCESS'){
                           obj.del();
                        }else{
                        	layer.msg(data.message);
                        } 
                    	
                    },"json");
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                //iframe层-父子操作
               var updateIndex = layer.open({
                    type: 2,
                    area: ['700px', '450px'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: '/resources/update?id=' + data.id
                });
               layer.full(updateIndex);
            }
        });

    });


</script>

</body>
</html>